(function($) {
	
	//设置AJAX的全局默认选项  
	$.ajaxSetup( {  
	    url: "/pages/login.html" , // 默认URL  
	    headers: { // 默认添加请求头  
	        "Author": "CodePlayer" ,  
	        "Powered-By": "CodePlayer"  
	    } ,  
	    error: function(jqXHR, textStatus, errorMsg){ // 出错时默认的处理函数  
	    	location.href='login.html';         
	    }  
	} ); 
	
    $.fn.zkTree = function(options) {
        var defaults = {
            treeId: null,
            parentIsNode: true,
            view: {
                dblClickExpand: false,
                selectedMulti: false
            },
            data: {
                key: {
                    name: "MC"
                },
                simpleData: {
                    enable: true,
                    idKey: "DM",
                    pIdKey: "SJDM"
                }
            },
            callback: {
                onClick: null
            },
            panel: {
                height: '45em'
            },
            tree_data: null
        };
        var settings = $.extend({},
        defaults, options);
        
        return this.each(function() {
            var $this = $(this);
            var random = Math.floor(Math.random() * 100) + 1;
            var treeId = "tree_" + random;
            if (settings.treeId) {
                treeId = settings.treeId;
            }
            var $zTree = $('<div  style="display:block;background-color: #f7f7f6;z-index:100;overflow: auto;">').css("height", settings.panel.height).append($('<ul class="ztree"></ul>').attr("id", treeId)).insertAfter($this);
            $this.click(function() {
                var width = $this.css("width");
                var cityOffset = $this.position();
                $this.next().css({
                    left: cityOffset.left,
                    top: cityOffset.top + $this.outerHeight()
                }).slideDown("fast").css("width", parseInt(width));
                $("body").bind("mousedown", onBodyDown);
            });
            function hideMenu() {
                $this.next().fadeOut("fast");
                $("body").unbind("mousedown", onBodyDown);
            };
            function onBodyDown(event) {
                if ($(event.target).closest("div.ztreePanel", $this).size() < 1) {
                    hideMenu();
                }
            };
            if (settings.async.enable) {
                $.fn.zTree.init($zTree.children(), settings);
            } else {
                $.fn.zTree.init($zTree.children(), settings, settings.tree_data);
            }
        });
    }
})(jQuery);